/**
 项目JS主入口
 **/
layui.define(['element', 'layer', 'table', 'form'], function(exports){
    var element = layui.element;
    var layer = layui.layer;
    var table = layui.table;
    var form = layui.form;

    var selNodeId;

    //导航树
    $.getJSON('../setting/resources/createResourceTree', {}, function (res) {
        if (res.success) {
            $('#resourceTree').jstree({
                'core': {
                    'data': res.tree
                }
            });
        }
    });

    //数据表格
    var resourceTable = table.render({
        elem: '#table',
        cols: [[
            {field: 'name', title: '资源名称', width: '150'},
            {field: 'code', title: '资源编码', width: '150'},
            {field: 'type', title: '资源类型', width: '100', align: 'center', renderer: function (value) {
                if (value == 'DIR') {
                    return '<a class="layui-btn layui-btn-mini">目录</a>';
                } else if (value == 'LINK') {
                    return '<a class="layui-btn layui-btn-mini">链接</a>';
                } else {
                    return '<a class="layui-btn layui-btn-mini">按钮</a>';
                }
            }},
            {field: 'icon', title: '资源图标', width: '100', align: 'center', renderer: function (value) {
                if (value) {
                    return '<i class="iconfont '+value+'"></i>';
                }
            }},
            {field: 'action', title: '资源链接', width: '200'},
            {field: 'permission', title: '资源权限', width: '250'},
            {field: 'isEnable', title: '状态', width: '100', align: 'center', renderer: function (value) {
                if (value == '1') {
                    return '<a class="layui-btn layui-btn-mini">可用</a>';
                } else {
                    return '<a class="layui-btn layui-btn-danger layui-btn-mini">禁用</a>';
                }
            }}
        ]],
        url: '../setting/resources/list',
        where: {parentId: '0'},
        page: true,
        limits: [15,20,25,30],
        limit: 20,
        toolbar: '#toolbar'
    });
    //点击树节点
    $('#resourceTree').on('select_node.jstree', function (node, selected, e) {
        selNodeId = selected.node.id;
        resourceTable.reload({where: {parentId: selNodeId}});
    });

    //新增
    var createForm = function (parentNode, html) {
        var formLayer = layer.open({
            type: 1,
            maxmin: true,
            area: ['800px', '600px'],
            content: html,
            success: function () {
                var parentId = parentNode.id;
                var parentName = parentNode.text;
                var parentType = parentNode.original.type;
                var type, typeName;
                if (parentId == '0') {
                    type = 'DIR';
                    typeName = '目录';
                } else if (parentType == 'DIR') {
                    type = 'LINK';
                    typeName = '链接';
                } else if (parentType == 'LINK') {
                    type = 'BUTTON';
                    typeName = '按钮';
                }
                $('#parentName').val(parentName);
                $('#parentId').val(parentId);
                $('#type').val(type);
                $('#typeName').val(typeName);
                form.render();
            }
        });

        form.on('submit(resource)', function (data) {
            $.ajax({
                url: '../../setting/resources',
                type: 'POST',
                dataType: 'JSON',
                data: data.field,
                success: function (res) {
                    if (res.success) {
                        layer.msg('保存成功', {time: 1000}, function () {
                            layer.close(formLayer);
                            resourceTable.reload({where: {parentId: selNodeId}});
                        });
                    } else {
                        layer.msg(res.message, {time: 3000});
                    }
                }
            });
            return false;
        });
    };

    $('#btn_add').on('click', function () {
        var node = $('#resourceTree').jstree('get_selected', true);
        if (!node || (node.length != 1)) {
            layer.msg('请选择一个父资源！', {time: 3000});
        } else {
            $.get('../setting/resources/new', {}, function (html) {
                createForm(node[0], html);
            })
        }
    });

    exports('resource', {}); //注意，这里是模块输出的核心，模块名必须和use时的模块名一致
});